<template>
  <n-select
    v-model:value="selectedValue"
    :disabled="currentDisabled"
    :placeholder="placeholder"
    :clearable="clearable"
    :multiple="multiple"
    :options="listData"
  />
</template>

<script setup lang="ts">
  import { useVModel } from '@vueuse/core';
  import { computed, inject, onBeforeMount, Ref, ref } from 'vue';
  import { useUser } from '@/store/modules/user';
  const userStore = useUser();

  interface EnumType {
    code: number;
    value: string;
  }
  type Props = {
    code: string;
    modelValue: Array<string | number> | string | number | null;
    multiple: boolean;
    clearable: boolean;
    placeholder: string;
    disabled: boolean;
    enumData: EnumType[];
  };
  const props = withDefaults(defineProps<Props>(), {
    code: '',
    modelValue: null,
    multiple: false,
    clearable: false,
    placeholder: '请选择',
    disabled: false,
    enumData: () => [],
  });
  const nForm: any = inject('n-form'); // 使用inject函数注入属性
  const currentDisabled = computed(() => {
    return props.disabled || nForm.props.disabled;
  });

  const emits = defineEmits(['update:modelValue']);

  const selectedValue = useVModel(props, 'modelValue', emits);

  const listData: Ref<EnumType[]> = ref([]);

  onBeforeMount(async () => {
    // 根据字典类型编码(typeCode)获取字典选项
    const enumData = userStore.getEnumData;
    if (props.enumData?.length > 0) {
      listData.value = props.enumData;
    } else {
      listData.value = enumData[props.code]
        ? enumData[props.code].map((item) => {
            return {
              value: item.code,
              label: item.value,
            };
          })
        : [];
    }
  });
</script>
